<?php

namespace App\Http\ViewComposers;

use App\Handlers\role;
use App\Models\Enum\Status;
use Illuminate\View\View;
use App\Services\Config\GamesService;

class GameComposer
{
    protected $channelService;

    public function __construct(GamesService $gamesService)
    {
        $this->gamesService = $gamesService;
    }

    public function compose(View $view)
    {
        $reqGameId = empty($view->reqGameId) ? old('game_id') : $view->reqGameId;
        $defaultCheck = $view->defaultCheck;
        $games = $this->gamesService->getGames();

        //拼出 渠道table
        $gameHtml = '';
        $select = '';
        foreach ($games as $key => $value) {
            $checked = '';

            if (
                (empty($reqGameId) && $defaultCheck !== false)
                || (isset($reqGameId) && in_array($value['game_id'], $reqGameId))) {
                $checked = 'checked="checked"';
            }

            $gameHtml .= <<<EOF
<div class="col-sm-1" style="min-width: 20rem">
    <label>&emsp;{$value['game_name']}
            <input type="checkbox" class="pull-left" $checked name="game_id[]" value="{$value['game_id']}">
    </label>
</div>
EOF;
        }
        $view->with(compact('gameHtml'));
    }
}